﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using StockMatch.Service;
using StockMatch.Service.Entity;
using StockMatch.WebSite.Models;

namespace StockMatch.WebSite.Controllers
{
    public class RegistController : StockControllerBase
    {
        public ActionResult Index()
        {
            UserData userData=new UserData();
            return View(userData);
        }

        [HttpPost]
        public ActionResult Index(UserData userData)
        {
            if (userData == null)
            {
                throw new ArgumentException("userData");
            }
            userData.RegistStep = 1;
            return View(userData);
        }

        /// <summary>
        /// 发送短信验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public ActionResult SendLockCode(string phone)
        {
            Response.SetCookie("MessageValidateCode", CommonFunc.SendLockCode(phone));
            return Json(new { code = 1 });
        }
        public ActionResult ValidateCode()
        {
            ValidateCode validateCode = new ValidateCode();
            validateCode.Width = 100;
            validateCode.Height = 35;
            //生成验证码指定的长度
            string code = validateCode.GetRandomString(4);
            Response.SetCookie("RegistValidateCode", code);
            //创建验证码的图片
            byte[] bytes = validateCode.CreateImage(code);
            //最后将验证码返回
            return File(bytes, @"image/jpeg");
        }

        [HttpPost]
        public ActionResult CheckUserName(string userName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException(userName);
            }
            var result= ServiceManager.UserService.IsExistUserName(userName);
            return Json(new { data = result });
        }
        [HttpPost]
        public ActionResult CheckUserPhone(string userHone)
        {
            if (string.IsNullOrEmpty(userHone))
            {
                throw new ArgumentNullException(userHone);
            }
            var result = ServiceManager.UserService.IsExistUserPhone(userHone);
            return Json(new { data = result });
        }

        [HttpPost]
        public ActionResult CheckValidateCode(string code)
        {
            string validateCode = Request.GetCookieValue("RegistValidateCode");
            int ret = 0;
            if (validateCode != null && code.ToLower() == validateCode.ToLower())
            {
                ret = 1;
            }
            return Json(new { data =ret });
        }

        [HttpPost]
        public ActionResult Check(UserData userData)
        {
            string validateCode = Request.GetCookieValue("MessageValidateCode");
            if (userData.CheckCode != validateCode)
            {
                return Json(new { data = 0 });
            }
            userData.RegistStep = 2;
            User user=new User();
            user.UserName = userData.UserName;
            user.UserPassword = Encryption.GetMD5HashHex(userData.UserPassword);
            user.Phone = userData.UserPhone;
            user.SafeGrade = 1;
            user.TotalAmount = 0;
            bool result= ServiceManager.UserService.Insert(user);
            return Json(new { data = result });
        }

        public ActionResult Seccess()
        {
            UserData userData=new UserData();
            userData.RegistStep = 2;
            return View("~/Views/Regist/Index.cshtml", userData);
        }

    }
}